home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / stevi69s.zip / SENTENCE.C < prev    next >
Text File  |  1990-04-23  |  5KB  |  219 lines

  1. /*    Find the NEXT/PREVIOUS:
  2.  *    - SENTENCE    findsent (dir)
  3.  *    - PARAGRAPH    findpara (dir)
  4.  *    - FUNCTION    findfunc (dir)
  5.  *
  6.  *    I've split these off from SEARCH.C, because they're alike and
  7.  *    SEARCH.C is a big file already.  findfunc() was already there.
  8.  *    I added findsent() and findpara().  -  Dave Tutelman
  9.  *  v1.1 Toad Hall Tweak, 20 Apr 90
  10.  */
  11.  
  12. #include "stevie.h"
  13. #include "ops.h"
  14.  
  15. /* We'll be doing some classification of input characters, into: */
  16. #define    BLANK    0    /* Whitespace */
  17. #define    DOT    1        /* Period, exclamation, q-mark */
  18. #define    EOL    2        /* End-of-line */
  19. #define    OTHER    3    /* Any other non-blank stuff */
  20.  
  21. extern    int    operator;    /* From normal.c, is there an operator
  22.                          * pending?
  23.                          */
  24.  
  25. int
  26. inclass (c)
  27.   char c;
  28. {
  29.     switch (c) {
  30.       case ' ':
  31.       case '\t':
  32.         return BLANK;
  33.       case '.':
  34.       case '!':
  35.       case '?':
  36.         return DOT;
  37.       case '\n':
  38.       case '\r':
  39.       case '\0':
  40.         return EOL;
  41.       default:
  42.         if (c<' ' || c>'~')    return EOL;
  43.         else            return OTHER;
  44.     }
  45. }
  46.  
  47. /* We'll also need to (1) tell if a line is just blanks, and
  48.  *                    (2) skip to the next OTHER character.
  49.  * Here are a couple of functions to do it.
  50.  */
  51.  
  52. bool_t
  53. blankline (line)
  54.   LPTR *line;
  55. {
  56.     char    *p;
  57.     int    class;
  58.  
  59.     if (! line)    return TRUE;
  60.     for (p = line->linep->s; (class=inclass(*p))!=EOL; p++)
  61.         if (class!=BLANK)    return FALSE;
  62.     return TRUE;
  63. }
  64.  
  65.  
  66. LPTR *
  67. skiptotext (lp, dir)
  68.   LPTR *lp;
  69.   int  dir;
  70. {
  71.     LPTR *lpp;
  72.  
  73.     lpp = lp;
  74.     while (inclass( CHAR( lpp )) != OTHER) {
  75.         lpp = (dir==FORWARD) ? nextchar (lpp) : prevchar (lpp);
  76.         if (!lpp) return (lp);        /* hit the end */
  77.     }
  78.     return (lpp);
  79. }
  80.  
  81.  
  82. /*
  83.  * findsent (dir) - Find the next sentence in direction 'dir'
  84.  *
  85.  * Return TRUE if a sentence was found.
  86.  *
  87.  * Algorithm: found end of a sentence if:
  88.  *   FWD - current char is BLANK | EOL and last is DOT.
  89.  *   BKWD- current char is DOT and last is BLANK | EOL.
  90.  * In either case, we then have to skip to text at beginning of next sentence.
  91.  *
  92.  */
  93. bool_t
  94. findsent (dir)
  95. int    dir;
  96. {
  97.     LPTR    *curr, *last;    /* LPTR for current & last characters */
  98.     int    ccurr, clast;        /* class of curr and last characters */
  99.     int    oldindex;            /* need to keep in case search fails */
  100.  
  101.     curr  = Curschar;
  102.     oldindex = curr->index;
  103.     /* Get INTO most recent sentence sentence. */
  104.     if (dir==BACKWARD)
  105.         curr = prevchar (curr);
  106.     curr = skiptotext (curr, BACKWARD);
  107.     ccurr = OTHER;
  108.  
  109.  
  110.     do {
  111.         /* Take a step */
  112.         last = curr; clast = ccurr;
  113.  
  114.         curr = (dir == FORWARD) ? nextchar(curr) : prevchar(curr);
  115.         ccurr = inclass (CHAR( curr ));
  116.  
  117.         /* Test halting condition */
  118.         if (dir==FORWARD &&
  119.             (ccurr==BLANK || ccurr==EOL) && clast==DOT) {
  120.             setpcmark();
  121.             last = skiptotext (last, FORWARD);
  122.             *Curschar = *last;
  123.             return TRUE;
  124.         }
  125.         else if (dir==BACKWARD &&
  126.              ccurr==DOT && (clast==BLANK || clast==EOL)) {
  127.             setpcmark();
  128.             last = skiptotext (last, FORWARD);
  129.             *Curschar = *last;
  130.             return TRUE;
  131.         }
  132.     } while (curr != NULL);
  133.  
  134.     Curschar->index = oldindex;    /* restore if search failed */
  135.     return FALSE;
  136. }
  137.  
  138.  
  139. /*
  140.  * findpara(dir) - Find the next paragraph in direction 'dir'
  141.  *
  142.  * Return TRUE if a paragraph was found.
  143.  *
  144.  * Algorithm: found beginning of paragraph if:
  145.  *   FWD - current line is non-blank and last is blank.
  146.  *   BKWD- current line is blank and last is non-blank.
  147.  * Then we skip to the first non-blank, non-dot text.
  148.  *
  149.  */
  150. bool_t
  151. findpara(dir)
  152. int    dir;
  153. {
  154.     LPTR    *curr, *last;    /* current & last lines */
  155. #ifdef NEVER_USED        /* v1.1 */
  156.     LPTR    *marker;        /* end of current para */
  157. #endif
  158.     bool_t    bcurr, blast;    /* "blankness" value for lines */
  159.  
  160.     curr  = Curschar;
  161.     bcurr = (dir==FORWARD) ? FALSE : TRUE;    /* keeps us from passing the
  162.                                              * text initially. */
  163.  
  164.     do {
  165.         /* Take a step */
  166.         last = curr; blast = bcurr;
  167.         curr = (dir == FORWARD) ? nextline(curr) : prevline(curr);
  168.         bcurr = blankline (curr);
  169.  
  170.         /* Test halting condition */
  171.         if (dir==FORWARD && bcurr && !blast) {
  172.             setpcmark();
  173.             curr = skiptotext (curr, FORWARD);
  174.             *Curschar = *curr;
  175.             return TRUE;
  176.         }
  177.         else if (dir==BACKWARD && bcurr && !blast) {
  178.             setpcmark();
  179.             last = skiptotext (last, FORWARD);
  180.             *Curschar = *last;
  181.             return TRUE;
  182.         }
  183.     } while (curr != NULL);
  184.  
  185.     return FALSE;
  186. }
  187.  
  188.  
  189. /*
  190.  * findfunc(dir) - Find the next function in direction 'dir'
  191.  *
  192.  * Return TRUE if a function was found.
  193.  *
  194.  * Algorithm depends on a style of C coding in which the ONLY '{'
  195.  * in the first column occurs at the beginning of a function definition.
  196.  * This is a good and common style, but not syntactically required by C.
  197.  */
  198. bool_t
  199. findfunc(dir)
  200. int    dir;
  201. {
  202.     LPTR    *curr;
  203.  
  204.     curr = Curschar;
  205.  
  206.     do {
  207.         curr = (dir == FORWARD) ? nextline(curr) : prevline(curr);
  208.  
  209.         if (curr != NULL && curr->linep->s[0] == '{') {
  210.             setpcmark();
  211.             *Curschar = *curr;
  212.             return TRUE;
  213.         }
  214.     } while (curr != NULL);
  215.  
  216.     return FALSE;
  217. }
  218.  
  219.